package com.aviccxzx.smarthome.ControlModule;

import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.aviccxzx.smarthome.Colorplate.ColorSelectDialog;
import com.aviccxzx.smarthome.CommonModule.GosBaseActivity;
import com.aviccxzx.smarthome.R;
import com.gizwits.gizwifisdk.api.GizWifiDevice;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizWifiDeviceListener;

import java.util.concurrent.ConcurrentHashMap;

public class GosDeviceControlActivity1 extends GosBaseActivity {

	/** The tv MAC */
	private TextView tvMAC;

	/** The GizWifiDevice device */
	private GizWifiDevice device;

	/** The ActionBar actionBar */
	ActionBar actionBar;

	/** 在云端创建的数据点标识名 */
	public static final String LIGHT_SWITCH = "switch";
	/** 开关灯Button */
	private Button btnLightSwitch;

	//----------------
	/** led灯拓展字段 3 */
	private static final String Light_Color = "Light_Color";

	/** led灯红色值 0-254. */
	private static final String KEY_LIGHT_RED = "LED_R";

	/** led灯绿色值 0-254. */
	private static final String KEY_LIGHT_GREEN = "LED_G";

	/** led灯蓝色值 0-254. */
	private static final String KEY_LIGHT_BLUE = "LED_B";


	/** The sb red. */
	private SeekBar sbRed;

	/** The sb green. */
	private SeekBar sbGreen;

	/** The sb blue. */
	private SeekBar sbBlue;

	/** The tv red. */
	private TextView tvRed;

	/** The tv green. */
	private TextView tvGreen;

	/** The tv blue. */
	private TextView tvBlue;
	/** The sb speed. */
	private SeekBar sbSpeed;

	private View view;
	private ColorSelectDialog colorSelectDialog;
	private int lastColor,red,green,blue;

	private Button choose_color;
//------------------

	/** 设备监听器 */
	private GizWifiDeviceListener deviceListener = new GizWifiDeviceListener() {
		// 接收数据回调
		public void didReceiveData(GizWifiErrorCode result, GizWifiDevice device,
								   ConcurrentHashMap<String, Object> dataMap, int sn) {
			// 已定义的设备数据点，有布尔、数值和枚举型数据
			if (dataMap.get("data") != null) {
				ConcurrentHashMap<String, Object> map = (ConcurrentHashMap<String, Object>) dataMap.get("data");
				// 根据标识名，在回调的map中找到设备上报的值
				if (map.get(LIGHT_SWITCH) != null) {
					boolean status = (Boolean) map.get(LIGHT_SWITCH);
					// 根据设备上报的值更改按钮的图标
					btnLightSwitch.setSelected(status);
				}
				if (map.get(KEY_LIGHT_RED) != null) {
					int red = (int) map.get(KEY_LIGHT_RED);
					// 根据设备上报的值更改按钮的图标
					//					btnLightSwitch.setSelected(status);
					Toast.makeText(GosDeviceControlActivity1.this,red+"--",Toast.LENGTH_LONG).show();
				}
				if (map.get(KEY_LIGHT_GREEN) != null) {
					int red = (int) map.get(KEY_LIGHT_GREEN);
					// 根据设备上报的值更改按钮的图标
					//					btnLightSwitch.setSelected(status);
					Toast.makeText(GosDeviceControlActivity1.this,green+"--",Toast.LENGTH_LONG).show();
				}
				if (map.get(KEY_LIGHT_BLUE) != null) {
					int red = (int) map.get(KEY_LIGHT_BLUE);
					// 根据设备上报的值更改按钮的图标
					//					btnLightSwitch.setSelected(status);
					Toast.makeText(GosDeviceControlActivity1.this,blue+"--",Toast.LENGTH_LONG).show();
				}

//				// 扩展数据点，key为"kuozhan"
//				byte[] bytes = (byte[]) map.get(Light_Color);
//				String string = bytesToHex(bytes);
//				Toast.makeText(GosDeviceControlActivity1.this, string+"扩展的", Toast.LENGTH_SHORT).show();
			}
		}
	};

	final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();

	public static String bytesToHex(byte[] bytes) {
		char[] hexChars = new char[bytes.length * 3];
		for (int j = 0; j < bytes.length; j++) {
			int v = bytes[j] & 0xFF;
			hexChars[j * 3] = hexArray[v >>> 4];
			hexChars[j * 3 + 1] = hexArray[v & 0x0F];
			hexChars[j * 3 + 2] = ' ';
		}
		return new String(hexChars);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gos_device_control1);
		initDevice();
		initView();
		initEvents();
	}

	private void initEvents() {
		choose_color.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
//				byte[] input1 = {(byte) 0xFE, (byte) 0xFF, (byte) 0xFF, 0x44, 0x55, 0x66, 0x77 };
//				char[] input1 = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
//				int[] input1 = { 255, 255, 255};
//				 byte[] input1 = { 11, 22, 33, 44, 55, 66, 77, 88, 99};
//				byte[] input2 = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
				//				int[] input2 = { 252, 253, 255};


				if (colorSelectDialog == null) {
					colorSelectDialog = new ColorSelectDialog(GosDeviceControlActivity1.this);
					colorSelectDialog.setOnColorSelectListener(new ColorSelectDialog.OnColorSelectListener() {
						@Override
						public void onSelectFinish(int color) {

							red= Color.red(color);
							green=Color.green(color);
							blue=Color.blue(color);

							int r=red-256;
							int g=green-256;
							int b=blue-256;

							byte[] input1 = {(byte) r, (byte)g, (byte)b};
//							byte[] input1 = {0, -128, -127};
							ConcurrentHashMap<String, Object> dataMap = new ConcurrentHashMap<String, Object>();
							dataMap.put(Light_Color, input1);
							device.write(dataMap, 0);

//														sendCommand(KEY_LIGHT_RED,r7ned);
//														sendCommand(KEY_LIGHT_GREEN,green);
//														sendCommand(KEY_LIGHT_BLUE,blue);

//							sendCommand(Light_Color,blue);

//							Toast.makeText(GosDeviceControlActivity1.this,red+"--"+green+"--"+blue,Toast.LENGTH_LONG).show();


//							char [] input1 = { 0XFF, 0XFF, 0XFF };
							// byte[] input2 = { 11, 22, 33, 44, 55, 66, 77, 88, 99};
//							ConcurrentHashMap<String, Object> dataMap = new ConcurrentHashMap<String, Object>();
//							dataMap.put(Light_Color, input1);
//							device.write(dataMap, 0);


						}
					});
				}
				colorSelectDialog.setLastColor(lastColor);
				colorSelectDialog.show();
			}
		});

		btnLightSwitch = (Button) findViewById(R.id.btn_light_onoff);
		btnLightSwitch.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				controlLight();
			}
		});

		sbRed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				sendCommand(KEY_LIGHT_RED, seekBar.getProgress());
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				tvRed.setText(progress + "");

			}
		});
		sbBlue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				sendCommand(KEY_LIGHT_BLUE, seekBar.getProgress());
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				tvBlue.setText(progress + "");

			}
		});
		sbGreen.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				sendCommand(KEY_LIGHT_GREEN, seekBar.getProgress());
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				tvGreen.setText(progress + "");

			}
		});
	}

	private void initView() {

		sbRed = (SeekBar) findViewById(R.id.sb_red);
		sbGreen = (SeekBar) findViewById(R.id.sb_green);
		sbBlue = (SeekBar) findViewById(R.id.sb_blue);

		tvRed = (TextView) findViewById(R.id.tv_red);
		tvGreen = (TextView) findViewById(R.id.tv_green);
		tvBlue = (TextView) findViewById(R.id.tv_blue);

		tvMAC = (TextView) findViewById(R.id.tvMAC);
		if (null != device) {

			tvMAC.setText(device.getMacAddress().toString());

		}

		choose_color=(Button)findViewById(R.id.choose_color);
	}

	/**
	 * Description::控制智能灯
	 */
	private void controlLight() {
		if (btnLightSwitch.isSelected()) {
			// 下发控制命令
			sendCommand(false);
			// 更改Button控件状态
			btnLightSwitch.setSelected(false);
		} else {
			sendCommand(true);
			btnLightSwitch.setSelected(true);
		}
	}

	/**
	 * Description:下发命令方法
	 *
	 * @param onOff
	 *            true表示开灯，false表示关灯
	 */
	private void sendCommand(boolean onOff) {
		int sn = 5;
		ConcurrentHashMap<String, Object> command = new ConcurrentHashMap<String, Object>();
		// map中key为云端创建数据点的标识名，value为需要传输的值
		command.put(LIGHT_SWITCH, onOff);
		// 调用write方法即可下发命令
		device.write(command, sn);
	}


	/**
	 * 发送指令
	 *
	 * @param key
	 *            数据点对应的标识名
	 * @param value
	 *            需要改变的值
	 */
	private void sendCommand(String key, Object value) {
		int sn = 5;
		ConcurrentHashMap<String, Object> hashMap = new ConcurrentHashMap<String, Object>();
		hashMap.put(key, value);
		device.write(hashMap, sn);
		Log.i("Apptest", hashMap.toString());
	}

	private void initDevice() {
		Intent intent = getIntent();
		device = (GizWifiDevice) intent.getParcelableExtra("GizWifiDevice");
		device.setListener(deviceListener);
		Log.i("Apptest", device.getDid());
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			this.finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		device.setSubscribe(false);
		device.setListener(null);
	}
}
